package cn.zcscloud.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.zcscloud.mall.common.CustomException;
import cn.zcscloud.mall.mbg.mapper.UmsRoleMapper;
import cn.zcscloud.mall.mbg.model.*;
import cn.zcscloud.mall.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class UmsRoleServiceImpl extends ServiceImpl<UmsRoleMapper, UmsRole> implements UmsRoleService {

    @Autowired
    private UmsRoleMenuRelationService umsRoleMenuRelationService;

    @Autowired
    private UmsRoleResourceRelationService umsRoleResourceRelationService;

    @Autowired
    private UmsMenuService umsMenuService;

    @Autowired
    private UmsResourceService umsResourceService;

    @Autowired
    private UmsAdminRoleRelationService umsAdminRoleRelationService;

    /**
     * 给角色分配菜单
     *
     * @param menuIds 分配的菜单id
     * @param roleId  角色id
     * @return
     */
    @Override
    public void allocMenu(List<Integer> menuIds, Integer roleId) {
        //先删除
        log.error("先删除======================={}",roleId);
        LambdaQueryWrapper<UmsRoleMenuRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleMenuRelation::getRoleId, roleId);
        umsRoleMenuRelationService.remove(lqw);
        //在添加
        log.error("在添加========== {}",menuIds);
        for (Integer menuId : menuIds) {
            UmsRoleMenuRelation umsRoleMenuRelation = new UmsRoleMenuRelation();
            umsRoleMenuRelation.setMenuId(menuId.longValue());
            umsRoleMenuRelation.setRoleId(roleId.longValue());
            umsRoleMenuRelationService.save(umsRoleMenuRelation);
        }
    }

    /**
     * 给角色分配资源
     *
     * @param resourceIds
     * @param roleId
     * @return
     */
    @Override
    public void allocResource(List<Integer> resourceIds, Integer roleId) {
        //先删除
        LambdaQueryWrapper<UmsRoleResourceRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleResourceRelation::getRoleId, roleId);
        umsRoleResourceRelationService.remove(lqw);
        //再添加
        for (Integer resourceId : resourceIds) {
            UmsRoleResourceRelation umsRoleResourceRelation = new UmsRoleResourceRelation();
            umsRoleResourceRelation.setRoleId(roleId.longValue());
            umsRoleResourceRelation.setResourceId(resourceId.longValue());
            umsRoleResourceRelationService.save(umsRoleResourceRelation);
        }
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteByIds(List<Integer> ids) {
        //判断角色是否关联菜单或者资源还有用户，如果关联了，不能删除，抛出异常
        for (Integer id : ids) {
            //菜单
            LambdaQueryWrapper<UmsRoleMenuRelation> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(UmsRoleMenuRelation::getRoleId, id);
            int count1 = umsRoleMenuRelationService.count(lqw1);
            //资源
            LambdaQueryWrapper<UmsRoleResourceRelation> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(UmsRoleResourceRelation::getRoleId, id);
            int count2 = umsRoleResourceRelationService.count(lqw2);
            //用户
            LambdaQueryWrapper<UmsAdminRoleRelation> lqw3 = new LambdaQueryWrapper<>();
            lqw3.eq(UmsAdminRoleRelation::getAdminId, id);
            int count3 = umsAdminRoleRelationService.count(lqw3);

            if (count1 > 0 || count2 > 0 || count3 > 0) {
                throw new CustomException("角色关联了其他选项，不能删除");
            }
        }
        //未关联，删除
        this.removeByIds(ids);
    }

    /**
     * 根据角色id获取角色相关菜单
     *
     * @param roleId
     * @return
     */
    @Override
    public List<UmsMenu> listMenu(Integer roleId) {
        //根据角色id查询出相关的菜单集合
        LambdaQueryWrapper<UmsRoleMenuRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleMenuRelation::getRoleId, roleId);
        List<UmsRoleMenuRelation> list = umsRoleMenuRelationService.list(lqw);
        if (CollUtil.isNotEmpty(list)) {
            //提取出来菜单id
            List<Long> menuIds = CollUtil.getFieldValues(list, "menuId", Long.class);
            //查询并返回
            return umsMenuService.listByIds(menuIds);
        }
        return null;
    }

    /**
     * 根据角色id获取角色相关资源
     *
     * @param roleId
     * @return
     */
    @Override
    public List<UmsResource> listResource(Integer roleId) {
        //根据角色id查询出相关的资源集合
        LambdaQueryWrapper<UmsRoleResourceRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleResourceRelation::getRoleId, roleId);
        List<UmsRoleResourceRelation> list = umsRoleResourceRelationService.list(lqw);
        if (CollUtil.isEmpty(list)){
            return null;
        }
        //提取出来菜单id
        List<Long> resourceIds = CollUtil.getFieldValues(list, "resourceId", Long.class);
        //查询并返回
        return umsResourceService.listByIds(resourceIds);
    }
}
